package hars

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order/catch"
	"order/models"
	"order/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	name := c.PostForm("name")
	password := c.PostForm("password")
	if name == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
			"data": nil,
		})
		return
	}
	redis_login := "redis_login" + name
	catchcount, _ := catch.Rdb.Get(catch.Ctx, redis_login).Int()
	if catchcount > 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "错误3次,禁止登录",
			"data": nil,
		})
		return
	}
	var user models.User
	err := models.DB.Where("name = ?", name).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名不正确",
			"data": nil,
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		catch.Rdb.Incr(catch.Ctx, redis_login)
		if catchcount == 2 {
			catch.Rdb.Set(catch.Ctx, redis_login, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 200,
				"msg":  "错误3次,封禁2小时",
				"data": nil,
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return

}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offst := (page - 1) * size

	var toal int64
	models.DB.Model(&models.Node{}).Count(&toal)

	var node []models.Node
	models.DB.Offset(offst).Limit(size).Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
		"toal": toal,
	})
	return
}
func RoleUser(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	data := models.UserRole{
		Uid: uid,
		Rid: rid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
func RloeNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
func CarAdd(c *gin.Context) {
	carname := c.PostForm("carname")
	data := models.Car{
		CarName: carname,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "车辆信息添加",
		"data": data,
	})
	return

}
func OrderAdd(c *gin.Context) {
	name := c.PostForm("name")
	data := models.Order{
		Name: name,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "订单添加",
		"data": data,
	})
	return
}
func Listl(c *gin.Context) {
	var order []models.Order
	redis_list := "redis_list"
	catchcount, _ := catch.Rdb.Get(catch.Ctx, redis_list).Result()
	if catchcount == "" {
		models.DB.Find(&order)
		jsondata, _ := json.Marshal(&order)
		catch.Rdb.Set(catch.Ctx, redis_list, jsondata, time.Hour*2)
	} else {
		json.Unmarshal([]byte(catchcount), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "允许按车辆/状态过滤",
		"data": order,
	})
	return

}
func Limit(c *gin.Context) {
	id := c.Query("id")
	var order models.Order
	redis_limit := "redis_limit" + id
	catchcount, _ := catch.Rdb.Get(catch.Ctx, redis_limit).Result()
	if catchcount == "" {
		models.DB.Where("id = ?", id).First(&order)
		jsondata, _ := json.Marshal(&order)
		catch.Rdb.Set(catch.Ctx, redis_limit, jsondata, time.Hour*2)
	} else {
		json.Unmarshal([]byte(catchcount), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "详情查询成功",
		"data": order,
	})
	return

}
